#!/usr/bin/env python
#-*- coding: utf-8 -*-
#__author__:vincentlc
#time: 16/6/5 : 11:02

'''
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

For example,
Given "egg", "add", return true.
Given "foo", "bar", return false.
Given "paper", "title", return true.

Note:
You may assume both s and t have the same length.
'''

class Solution(object):
    def isIsomorphic(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        list_s = list(s)
        list_t = list(t)

        if len(list_s) != len(list_t):
            return False
        else:
            s_dict = dict()
            t_dict = dict()
            for i,s_item in enumerate(list_s):
               if s_item not in s_dict:
                   if list_t[i] not in t_dict:
                       s_dict[s_item] = list_t[i]
                       t_dict[list_t[i]] = s_item
                   elif t_dict[list_t[i]]!= s_item:
                       return False
               elif s_item in s_dict:
                   if s_dict[s_item] != list_t[i] or t_dict[list_t[i]]!= s_item:
                       return False
                   else:
                       continue
        return True

cls_a = Solution()

s = "ab"
t = "aa"
print(cls_a.isIsomorphic(s,t))

s = "ab"
t = "ca"
print(cls_a.isIsomorphic(s,t))
